﻿using System;
using Microsoft.Practices.Unity;

namespace ServiceLocatorPatternDemo
{
    class Program
    {
        static void Main(string[] args)
        {  
            // Step 1 : Create an instance of Unity container. Here it will acts as a Service Locator.
            IUnityContainer UnityContainerObject = new UnityContainer();

            // Step 2 : Use UnityContainerObject and register the ILogger.
            UnityContainerObject.RegisterType<ILogger, ILogger>();

            // Step 2 : Use UnityContainerObject's Resolve method and get instance of DemoTextLogger.
            // Focus more on this step and understand clearly, we are not directly creating instance of DemoTextLogger.
            // So if there is any change in DemoTextLogger (let us assume we are consuming from outside of our project), we will not requireded to change our project code.
            ILogger ILoggerInstance = UnityContainerObject.Resolve<DemoTextLogger>();

            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("UnityContainer created the object of type DemoTextLogger"));


            // Step 3 : Use UnityContainerObject's Resolve method and get instance of DemoExceptionLogger.
            // Focus more on this step and understand clearly, we are not directly creating instance of DemoExceptionLogger.
            // So if there is any change in DemoExceptionLogger (let us assume we are consuming from outside of our project), we will not requireded to change our project code.
            ILoggerInstance = UnityContainerObject.Resolve<DemoExceptionLogger>();

            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("UnityContainer created the object of type DemoExceptionLogger"));

            Console.ReadLine();
        }
    }
}